import { createRouter, createWebHashHistory } from "vue-router";
// import Home from "../views/Home.vue";

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    redirect: "/home/homepage",
    component: () => import("../views/Home.vue"),
    children: [
      {
        //首页
        path: "/home/homepage",
        component: () => import("../components/home/Homepage.vue"),
      },
      {
        //社区页
        path: "/home/community",
        component: () => import("../components/home/Community.vue"),
        meta: { requiresAuth: true },
      },
      {
        //会员页
        path: "/home/member",
        component: () => import("../components/home/Member.vue"),
        meta: { requiresAuth: true },
      },
      {
        //商城页
        path: "/home/shoppingmall",
        component: () => import("../components/home/Shoppingmall.vue"),
        meta: { requiresAuth: true },
      },
      {
        //我的页
        path: "/home/mine",
        component: () => import("../components/home/Mine.vue"),
        meta: { requiresAuth: true },
      },
    ],
  },
  //专家服务
  {
    //首页-专家服务
    path: "/expertservice",
    component: () => import("../views/ExpertService.vue"),
    meta: { requiresAuth: true },
  },
  //专家搜索
  {
    path: "/expertsearch",
    component: () => import("../views/Expertsearch.vue"),
  },
  {
    //商品详情页
    path: "/detail/:id",
    component: () => import("../views/Detail.vue"),
  },
  {
    //商城-家中常备
    path: "/shoplist",
    component: () => import("../views/ShopList.vue"),
  },
  {
    //首页-疾病百科
    path: "/homedisease",
    component: () => import("../views/HomeDisease.vue"),
    meta: { requiresAuth: true },
  },
  {
    //首页-找医生
    path: "/homedoctor",
    component: () => import("../views/HomeDoctor.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/login",
    redirect: "/login/smslogin",
    component: () => import("../views/Login.vue"),
    children: [
      {
        path: "/login/smslogin",
        component: () => import("../views/Login/SMSLogin.vue"),
      },
      {
        path: "/login/accountlogin",
        component: () => import("../views/Login/AccountLogin.vue"),
      },
    ],
  },
  {
    //我的-企业会员
    path: "/corporateMem",
    component: () => import("../views/CorporateMem.vue"),
  },
  //我的-设置
  {
    //我的-企业会员
    path: "/setup",
    component: () => import("../views/Setup.vue"),
  },
  {
    //短信验证
    path: "/verify",
    component: () => import("../views/Verify.vue"),
  },
  {
    //我的-搜索页
    path: "/homesearch",
    component: () => import("../views/Homesearch.vue"),
  },
  {
    //首页-消息中心
    path: "/homemessage",
    component: () => import("../views/HomeMessage.vue"),
    meta: { requiresAuth: true },
  },
  {
    //404找不到页面。。
    path: "/:pathMatch(.*)*",
    component: () => import("../views/NotFound.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, form, next) => {
  // console.log();
  if (to.meta.requiresAuth) {
    // console.log(sessionStorage.getItem("token"));
    if (sessionStorage.getItem("token") !== null) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

export default router;
